#include "calculator.h"
#include "ui_calculator.h"

Calculator::Calculator(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("简易计算器");
    this->setWindowIcon(QIcon(":/icon.png"));
}

Calculator::~Calculator()
{
    delete ui;
}

void Calculator::init(void)
{
    //不通过各个按键的信号槽进行处理，而是收集各个按键的名字，通过名字进行对应的操作，避免太多信号槽
    auto btn_group = new QButtonGroup(this);
    auto btn_list = this->findChildren<QPushButton*>();
    for(auto btn : btn_list)
    {
        btn_group->addButton(btn);
    }

    void (QButtonGroup:: *signal_checked)(QAbstractButton *) = &QButtonGroup::buttonClicked;    //信号发生重载，两种方法解决   QT5
    connect(btn_group, signal_checked, this, &Calculator::btn_checked);

    //一些变量初始化
    val1 = val2 = result = 0.0;
    opt.clear();
    coiled = false;
}


//按键统一处理
void Calculator::btn_checked(QAbstractButton *btn)
{
    qDebug()<< btn->text();
    QString name = btn->text();

    //数字
    if(name >= "0" && name <= "9")
    {
        if(ui->lineEdit_this->text() == "0")
            ui->lineEdit_this->clear();

        ui->lineEdit_this->insert(name);
    }

    else if(name == ".")
    {
        if(ui->lineEdit_this->text() == "0" && name!= ".")  //判断是否刚打开，刚打开只有0，如果刚打开后输入小数点，要显示0在前面，否则都要清空
            ui->lineEdit_this->clear();

        if(ui->lineEdit_this->text().contains(".") == false)
            ui->lineEdit_this->insert(name);
    }

    //删除
    else if(name == "<")
    {
        ui->lineEdit_this->setCursorPosition(ui->lineEdit_this->cursorPosition()-1);
        ui->lineEdit_this->del();

        if(ui->lineEdit_this->text().isEmpty())
            ui->lineEdit_this->setText("0");
    }

    //清空
    else if(name == "AC")
    {
        coiled = false;
        opt.clear();
        val1 = val2 = result = 0.0;
        ui->lineEdit_last->clear();
        ui->lineEdit_this->clear();
        ui->lineEdit_this->setText("0");
    }

    else if(name == "+")
    {
        if(!ui->lineEdit_this->text().isEmpty())
        {
            val1 = ui->lineEdit_this->text().toDouble();    //第一个数字
            opt = name;

            //公式上移显示
            ui->lineEdit_last->setText(ui->lineEdit_this->text() + opt);
            ui->lineEdit_this->setText("0");
        }
    }

    else if(name == "-")
    {
        if(!ui->lineEdit_this->text().isEmpty())
        {
            val1 = ui->lineEdit_this->text().toDouble();    //第一个数字
            opt = name;

            //公式上移显示
            ui->lineEdit_last->setText(ui->lineEdit_this->text() + opt);
            ui->lineEdit_this->setText("0");
        }
    }

    else if(name == "×")
    {
        if(!ui->lineEdit_this->text().isEmpty())
        {
            val1 = ui->lineEdit_this->text().toDouble();    //第一个数字
            opt = name;

            //公式上移显示
            ui->lineEdit_last->setText(ui->lineEdit_this->text() + opt);
            ui->lineEdit_this->setText("0");
        }
    }

    else if(name == "÷")
    {
        if(!ui->lineEdit_this->text().isEmpty())
        {
            val1 = ui->lineEdit_this->text().toDouble();    //第一个数字
            opt = name;

            //公式上移显示
            ui->lineEdit_last->setText(ui->lineEdit_this->text() + opt);
            ui->lineEdit_this->setText("0");
        }
    }

    else if(name == "=")
    {
        if(!ui->lineEdit_last->text().isEmpty())
        {
            //采集第二个数字
            if(coiled == false)
                val2 = ui->lineEdit_this->text().toDouble();

            //更新上行显示
            ui->lineEdit_last->setText(QString::number(val1) + opt +QString::number(val2) + "=");

            //计算
            if(opt == "+")
            {
                result = val1 + val2;
            }
            else if(opt == "-")
            {
                result = val1 - val2;
            }
            else if(opt == "×")
            {
                result = val1 * val2;
            }
            else if(opt == "÷")
            {
                result = val1 / val2;
            }

            //显示结果
            ui->lineEdit_this->setText(QString::number(result));

            //连续等于
            coiled = true;
            val1 = result;
        }
    }
}

void Calculator::keyPressEvent(QKeyEvent *event)
{
    qDebug()<< event->text();
    QString name = event->text();

    //数字
    if(name >= "0" && name <= "9")
    {
        if(ui->lineEdit_this->text() == "0")
            ui->lineEdit_this->clear();

        ui->lineEdit_this->insert(name);
    }

    else if(name == ".")
    {
        if(ui->lineEdit_this->text() == "0" && name!= ".")  //判断是否刚打开，刚打开只有0，如果刚打开后输入小数点，要显示0在前面，否则都要清空
            ui->lineEdit_this->clear();

        if(ui->lineEdit_this->text().contains(".") == false)
            ui->lineEdit_this->insert(name);
    }

    //删除
    else if(name == "<" || name == "\b")
    {
        ui->lineEdit_this->setCursorPosition(ui->lineEdit_this->cursorPosition()-1);
        ui->lineEdit_this->del();

        if(ui->lineEdit_this->text().isEmpty())
            ui->lineEdit_this->setText("0");
    }

    //清空
    else if(name == "AC" || name == "\u007F")
    {
        coiled = false;
        opt.clear();
        val1 = val2 = result = 0.0;
        ui->lineEdit_last->clear();
        ui->lineEdit_this->clear();
        ui->lineEdit_this->setText("0");
    }

    else if(name == "+")
    {
        if(!ui->lineEdit_this->text().isEmpty())
        {
            val1 = ui->lineEdit_this->text().toDouble();    //第一个数字
            opt = name;

            //公式上移显示
            ui->lineEdit_last->setText(ui->lineEdit_this->text() + opt);
            ui->lineEdit_this->setText("0");
        }
    }

    else if(name == "-")
    {
        if(!ui->lineEdit_this->text().isEmpty())
        {
            val1 = ui->lineEdit_this->text().toDouble();    //第一个数字
            opt = name;

            //公式上移显示
            ui->lineEdit_last->setText(ui->lineEdit_this->text() + opt);
            ui->lineEdit_this->setText("0");
        }
    }

    else if(name == "×")
    {
        if(!ui->lineEdit_this->text().isEmpty())
        {
            val1 = ui->lineEdit_this->text().toDouble();    //第一个数字
            opt = name;

            //公式上移显示
            ui->lineEdit_last->setText(ui->lineEdit_this->text() + opt);
            ui->lineEdit_this->setText("0");
        }
    }

    else if(name == "÷")
    {
        if(!ui->lineEdit_this->text().isEmpty())
        {
            val1 = ui->lineEdit_this->text().toDouble();    //第一个数字
            opt = name;

            //公式上移显示
            ui->lineEdit_last->setText(ui->lineEdit_this->text() + opt);
            ui->lineEdit_this->setText("0");
        }
    }

    else if(name == "=")
    {
        if(!ui->lineEdit_last->text().isEmpty())
        {
            //采集第二个数字
            if(coiled == false)
                val2 = ui->lineEdit_this->text().toDouble();

            //更新上行显示
            ui->lineEdit_last->setText(QString::number(val1) + opt +QString::number(val2) + "=");

            //计算
            if(opt == "+")
            {
                result = val1 + val2;
            }
            else if(opt == "-")
            {
                result = val1 - val2;
            }
            else if(opt == "×")
            {
                result = val1 * val2;
            }
            else if(opt == "÷")
            {
                result = val1 / val2;
            }

            //显示结果
            ui->lineEdit_this->setText(QString::number(result));

            //连续等于
            coiled = true;
            val1 = result;
        }
    }

}



